package com.example.demo.controller;

import com.example.demo.common.Result;
import com.example.demo.common.StatusCode;
import com.example.demo.model.Nutritionalvalue;
import com.example.demo.service.NutritionalValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
public class NutritionalValueController {

    @Autowired
    private NutritionalValueService nutritionalValueService;

    @GetMapping("/dishes/nutritionalvalue/{name}")
    public ResponseEntity<Result<Nutritionalvalue>> getDishNutritionalValue(@PathVariable String name) {
        Nutritionalvalue nutritionalValue = nutritionalValueService.getDishNutritionalValue(name);
        if (nutritionalValue != null) {
            return ResponseEntity.ok(new Result<>(true, StatusCode.OK, "查询成功", nutritionalValue));
        } else {
            return ResponseEntity.ok(new Result<>(false, StatusCode.ERROR, "未找到对应菜品的营养价值信息", null));
        }
    }

    @GetMapping("/ingredient/nutritionalvalue/{name}")
    public ResponseEntity<Result<Nutritionalvalue>> getIngredientNutritionalValue(@PathVariable String name) {
//        String name = requestBody.get("name");
        System.out.println(name);
        if (name != null && !name.isEmpty()) {
            Nutritionalvalue nutritionalValue = nutritionalValueService.getIngredientNutritionalValue(name);
            if (nutritionalValue != null) {
                System.out.println(nutritionalValue);
                return ResponseEntity.ok(new Result<>(true, StatusCode.OK, "查询成功", nutritionalValue));
            } else {
                return ResponseEntity.ok(new Result<>(false, StatusCode.ERROR, "未找到对应食材的营养价值信息", null));
            }
        } else {
            return ResponseEntity.ok(new Result<>(false, StatusCode.ERROR, "请求参数错误", null));
        }
    }
}
